﻿using Apps.Common;
using Apps.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Apps.DAL.YANX
{
    public partial class YANX_RECORD_Repository : DbContext<YANX_RECORD>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public YANX_RECORD Info(string id)
        {
            return Db.Queryable<YANX_RECORD>().Where(m => m.YAN_ID == id).First();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="queryStr"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sortFiled"></param>
        /// <param name="orderBy"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<YANX_RECORD> GetList(string batchCode, string testCode, string name, int pageIndex, int pageSize, string sortFiled, string orderBy, ref int totalCount)
        {
            var exp = Expressionable.Create<YANX_RECORD>();
            exp.AndIF(!string.IsNullOrEmpty(batchCode), it => it.BATCH_CODE.Contains(batchCode));
            exp.AndIF(!string.IsNullOrEmpty(testCode), it => it.TEST_CODE.Contains(testCode));
            exp.AndIF(!string.IsNullOrEmpty(name), it => it.ITEM_NAME.Contains(name));
            return Db.Queryable<YANX_RECORD>().OrderBy(sortFiled + " " + orderBy).Where(exp.ToExpression()).ToPageList(pageIndex, pageSize, ref totalCount);
        }

        /// <summary>
        /// 根据批次号 获取所有岩相文件
        /// </summary>
        /// <param name="batchCode"></param>
        /// <returns></returns>
        public List<YANX_RECORD> GetAllList(string batchCode)
        {
            var exp = Expressionable.Create<YANX_RECORD>();
            exp.AndIF(!string.IsNullOrEmpty(batchCode), it => it.BATCH_CODE == batchCode);
            exp.And(m => m.AUDIT_STATUS == "审核通过");
            return Db.Queryable<YANX_RECORD>().Where(exp.ToExpression()).ToList();
        }

        /// <summary>
        /// 根据制样码获取批次码
        /// </summary>
        /// <returns></returns>
        public string GetBatchCode(string testCode)
        {
            return Db.Ado.GetString("SELECT MAX(BATCH_CODE) BATCH_CODE FROM LMS_COLLECTSAMPLE WHERE TEST_CODE = @TEST_CODE", new SugarParameter[] { new SugarParameter("@TEST_CODE", testCode) });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Add(YANX_RECORD model)
        {
            return Db.Insertable<YANX_RECORD>(model).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Update(YANX_RECORD model)
        {
            return Db.Updateable<YANX_RECORD>(model).UpdateColumns(m => new
            {
                m.ITEM_NAME,
                m.ITEM_VALUE
            }).Where(m => m.YAN_ID == model.YAN_ID).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(string id)
        {
            return Db.Deleteable<YANX_RECORD>().Where(m => m.YAN_ID == id).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 批量审核
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="examine"></param>
        /// <returns></returns>
        public bool ExamineData(List<string> ids, string examine)
        {
            try
            {
                Db.BeginTran();
                ids.ForEach(res =>
                {
                    var yxData = Db.Queryable<YANX_RECORD>().Where(m => m.YAN_ID == res).First();
                    if (yxData != null)
                    {
                        if (!string.IsNullOrEmpty(yxData.YAN_ID))
                        {
                            //判断此岩相数据对应 自采煤批次是否已经  在关联表，如果存在则修改  他的岩相数据
                            var value = "";
                            if (examine == "审核通过")
                                value = yxData.ITEM_VALUE;
                            else
                                value = "";

                            if (Db.Queryable<SELF_SAMPLING_RC_RELATED>().Where(m => m.XJLM_CODE == yxData.TEST_CODE).Any())
                            {
                                Db.Updateable<SELF_SAMPLING_RC_RELATED>(new SELF_SAMPLING_RC_RELATED { YX_VALUE = value }).UpdateColumns(m => new { m.YX_VALUE })
                                .Where(m => m.XJLM_CODE == yxData.TEST_CODE).ExecuteCommand();
                            }
                            if (Db.Queryable<SELF_SAMPLING_RC_RELATED>().Where(m => m.XJLJT_CODE == yxData.TEST_CODE).Any())
                            {
                                Db.Updateable<SELF_SAMPLING_RC_RELATED>(new SELF_SAMPLING_RC_RELATED { YX_VALUE = value }).UpdateColumns(m => new { m.YX_VALUE })
                                .Where(m => m.XJLM_CODE == yxData.TEST_CODE).ExecuteCommand();
                            }
                            Db.Updateable<YANX_RECORD>(new YANX_RECORD
                            {
                                AUDIT_STATUS = examine,
                                AUDIT_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                            }).UpdateColumns(m => new { m.AUDIT_STATUS, m.AUDIT_TIME }).Where(m => m.YAN_ID == res).ExecuteCommand();
                        }
                    }
                });
                Db.CommitTran();
                return true;
            }
            catch
            {
                Db.RollbackTran();
                return false;
            }
        }

        /// <summary>
        /// 更改采样状态
        /// </summary>
        /// <param name="samplingId"></param>
        /// <param name="examine"></param>
        /// <returns></returns>
        public bool ExamineError(string samplingId, bool isError)
        {
            try
            {
                return Db.Updateable<Squgar_RC_SAMPLING>(new Squgar_RC_SAMPLING { ISERROR = isError }).UpdateColumns(m => new { m.ISERROR }).Where(m => m.SAMPLING_ID == samplingId).ExecuteCommand() > 0;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 补录 样桶卡号 信息
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="examine"></param>
        /// <returns></returns>
        public bool BuChong(string samplingId, string rfid, string weight = "0", string isShouyang = "")
        {
            try
            {
                Db.BeginTran();
                decimal SAMPLE_WEIGHT = 0;
                if (!string.IsNullOrEmpty(weight))
                    decimal.TryParse(weight, out SAMPLE_WEIGHT);
                Db.Updateable<Squgar_RC_SAMPLING>(
                     new Squgar_RC_SAMPLING
                     {
                         RFID = rfid
                     })
                 .UpdateColumns(m => new { m.RFID }).Where(m => m.SAMPLING_ID == samplingId).ExecuteCommand();
                if (SAMPLE_WEIGHT > 0)
                    Db.Updateable<Squgar_RC_SAMPLING>(
                    new Squgar_RC_SAMPLING
                    {
                        SAMPLE_WEIGHT = SAMPLE_WEIGHT
                    })
                .UpdateColumns(m => new
                {
                    m.SAMPLE_WEIGHT
                }).Where(m => m.SAMPLING_ID == samplingId).ExecuteCommand();
                if (isShouyang == "设为已收样")
                    Db.Updateable<Squgar_RC_SAMPLING>(
                        new Squgar_RC_SAMPLING
                        {
                            REVICED = true,
                            ISERROR = false,
                            MADE = true,
                            BLENDED = true,
                        }).UpdateColumns(m => new
                        {
                            m.REVICED,
                            m.ISERROR,
                            m.MADE,
                            m.BLENDED
                        }).Where(m => m.SAMPLING_ID == samplingId).ExecuteCommand();
                Db.CommitTran();
                return true;
            }
            catch (Exception ex)
            {
                Db.RollbackTran();
                return false;
            }
        }

    }
}
